package com.coderlin.controller;

import com.coderlin.entities.Worker;
import com.coderlin.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <Description> <br>
 *
 * @author Coder_Lin<br>
 * @version 1.0<br>
 * @taskId: <br>
 * @createDate 2021/07/23 16:52 <br>
 * @see com.coderlin.controller <br>
 */
@Controller
@RequestMapping("/worker")
public class WokerController {

    @Autowired
    private WorkerService workerService;

    @RequestMapping("/listWorker")
    public String listWorker(Model model){
        List<Worker> workers = workerService.listWorker();
        model.addAttribute("workerList",workers);
        return "workerList";
    }

    @RequestMapping("/searchWorker")
    public String searchWorker(String workerName, Model model){
        List<Worker> workers = workerService.searchWorker(workerName);
        model.addAttribute("workerList",workers);
        return "workerList";
    }

    @RequestMapping("/addWorkerPre")
    public String addWorkerPre(){
        return "workerAdd";
    }

    @RequestMapping("/addWorker")
    public String addWorker(Worker worker){
        Integer integer = workerService.addWorker(worker);
        //添加失败，弹窗
        if(integer<=0){

        }
        return "redirect:/worker/listWorker";
    }

    @RequestMapping("/delWorker/{wid}")
    public String delWorker(@PathVariable("wid") int wid){
        Integer integer = workerService.delWorkerById(wid);
        if(integer<=0){

        }
        return "redirect:/worker/listWorker";
    }

    @RequestMapping("/modWorkerPre/{wid}")
    public String modWorkerPre(@PathVariable("wid") int wid,Model model){
        //依照id查找worker
        Worker worker = workerService.searchWorkerById(wid);
        model.addAttribute("wid",wid);
        model.addAttribute("worker",worker);
        return "workerMod";
    }

    @RequestMapping("/modWorker")
    public String modWorker(Worker worker){
        Integer integer = workerService.modWorker(worker);

        if(integer<=0){

        }
        return "redirect:/worker/listWorker";
    }

    @RequestMapping(value = "/adjustSalary",method = RequestMethod.POST)
    @ResponseBody
    public String adjustSalary(Integer wid,Integer workerSalary){
        Integer integer = workerService.adjustSalary(wid, workerSalary);
        return integer == 1 ? "success":"fail";
    }

}
